<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="orderNo" label="订单编号">
              <el-input v-model="filters.orderNo" clearable></el-input>
            </el-form-item>
            <el-form-item prop="billType" label="账单类型">
              <el-select v-model="filters.billType" clearable>
                <el-option v-for="item in billTypeList" :label="item.name" :value="item.id" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="date" label="时间范围">
              <el-date-picker v-model="filters.date" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
            </el-form-item>
            <el-form-item label="金额范围">
              <div class="amount-wrapper">
                <el-input type="number" v-model="filters.beginAmount" placeholder="最少金额"> </el-input>
                <div class="separate"></div>
                <el-input type="number" v-model="filters.endAmount" placeholder="最多金额"> </el-input>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="createDate" align="center" width="100"> </el-table-column>
        <el-table-column label="订单编号" prop="orderNo" align="center"> </el-table-column>
        <el-table-column label="账单类型" prop="type" align="center">
          <template slot-scope="scope">
            <el-tag :type="formatBillTypes(scope.row.billType)" effect="dark" size="small">
              {{ formatBillType(scope.row.billType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="总金额" prop="amount" align="center"> </el-table-column>
        <el-table-column label="平台所得" prop="platformGain" align="center"> </el-table-column>
        <el-table-column label="平台支出" prop="platformPay" align="center"> </el-table-column>
        <el-table-column label="商户所得" prop="businessGain" align="center"> </el-table-column>
        <el-table-column label="商户支出" prop="businessPay" align="center"> </el-table-column>
        <el-table-column label="服务员所得" prop="waiterGain" align="center"> </el-table-column>
        <el-table-column label="服务员支出" prop="waiterPay" align="center"> </el-table-column>
        <el-table-column label="用户所得" prop="userGain" align="center"> </el-table-column>
        <el-table-column label="用户支出" prop="userPay" align="center"> </el-table-column>
        <el-table-column label="推广员所得" prop="promoterGain" align="center"> </el-table-column>
        <el-table-column label="推广员支出" prop="promoterPay" align="center"> </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import PlatformBillApi from '@/api/account/platformBill.js';
import * as message from '@/constants/message';
export default {
  name: 'PlatformBillManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        orderNo: '',
        date: [],
        beginAmount: '',
        endAmount: '',
        billType: '',
      },
      billTypeList: [
        { id: 1, name: '线上下单' },
        { id: 10, name: '实体下单' },
        { id: 20, name: '充值' },
        { id: 30, name: '取消退款' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    formatBillType(val) {
      switch (val) {
        case 1:
          return '线上下单';
        case 10:
          return '实体下单';
        case 20:
          return '充值';
        case 30:
          return '取消退款';
      }
    },
    formatBillTypes(val) {
      switch (val) {
        case 1:
          return 'success';
        case 10:
          return '';
        case 20:
          return 'danger';
        case 30:
          return 'info';
      }
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    resetForm() {
      this.filters.beginAmount = '';
      this.filters.endAmount = '';
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
      };

      cond.orderNo = _this.filters.orderNo;
      cond.billType = _this.filters.billType;
      if (_this.filters.date.length > 0) {
        cond.beginDate = _this.$Utils.formatDate(this.filters.date[0], 'yyyy-MM-dd hh:mm:ss');
        cond.endDate = this.$Utils.formatDate(this.filters.date[1], 'yyyy-MM-dd hh:mm:ss');
      }
      let beginAmount = _this.filters.beginAmount;
      let endAmount = _this.filters.endAmount;
      if (beginAmount.length > 0 && endAmount.length > 0) {
        if (parseFloat(beginAmount) > parseFloat(endAmount)) {
          this.$message.error('最大金额必须大于最小金额');
          return;
        }
      }
      cond.beginAmount = _this.filters.beginAmount;
      cond.endAmount = _this.filters.endAmount;
      this.listLoading = true;
      PlatformBillApi.billList(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.listLoading = false;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.amount-wrapper {
  display: flex;
  align-items: center;

  .el-input {
    width: 200px;
  }

  .el-input,
  .separate {
    flex-shrink: 0;
  }

  .separate {
    width: 14px;
    height: 2px;
    margin: 0 4px;
    background-color: #dcdfe6;
  }
}
</style>
